package com.niuke;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 56. 战场索敌
 * 和 50. 寻找最大价值的矿堆 解法完全相同
 * https://zhuanlan.zhihu.com/p/647297702
 */
public class NiukeMoni56 {

    public static int[][] offsets = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static int row;
    public static int col;
    public static String[][] data;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String[] line1 = scanner.nextLine().split(" ");
        row = Integer.parseInt(line1[0]); // 几行
        col = Integer.parseInt(line1[1]); // 几列
        int k = Integer.parseInt(line1[2]); // 敌人数小于k

        data = new String[row][col];
        for (int i = 0; i < row; i++) {
            data[i] = scanner.nextLine().split(" ");
        }

        List<Integer> numberResult = new ArrayList<>();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (data[i][j].equals("#")) {
                    continue;
                }

                int ans = dfs(i, j);
                numberResult.add(ans);
            }
        }

        int result = 0;
        for (int i = 0; i < numberResult.size(); i++) {
            if (i == numberResult.size() - 1) {
                System.out.println(numberResult.get(i));
            } else {
                System.out.print(numberResult.get(i) + " ");
            }
            if (numberResult.get(i) < k) {
                result++;
            }
        }
        System.out.println(result);
    }

    public static int dfs(int x, int y) {
        if (data[x][y].equals("#")) {
            return 0;
        }
        int ans = 0;
        if (data[x][y].equals("E")) {
            ans = 1;
        }
        data[x][y] = "#";

        for (int[] offset : offsets) {
            int xx = x + offset[0];
            int yy = y + offset[1];
            if (xx >= 0 && xx < row && yy >= 0 && yy < col) {
                ans += dfs(xx, yy);
            }
        }
        return ans;
    }

}
